package com.ziyun.erp.web.api.quoteFastPrint;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.ziyun.erp.common.e.DeleteEnum;
import com.ziyun.erp.common.e.quote.QuoteFastPrintColourEnum;
import com.ziyun.erp.common.e.quote.QuotePrintFactoryEnum;
import com.ziyun.erp.common.utils.JsonUtils;
import com.ziyun.erp.common.utils.UserUtils;
import com.ziyun.erp.modules.quoteFastPrint.entity.QuoteFastPrintEntity;
import com.ziyun.erp.modules.quoteFastPrint.service.QuoteFastPrintService;
import com.ziyun.erp.modules.quoteFastPrint.vo.QuoteFastPrintEntityVo;
import com.ziyun.erp.modules.quotePrint.entity.QuotePrintEntity;
import com.ziyun.erp.modules.quotePrint.service.QuotePrintService;
import com.ziyun.erp.modules.quotePrintFactoryPrice.entity.QuotePrintFactoryPriceEntity;
import com.ziyun.erp.modules.quotePrintFactoryPrice.service.QuotePrintFactoryPriceService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 快印印刷机配置表
 *
 * @author llc
 * @email 
 * @date 2018-09-10 18:44:20
 */
@RestController
@RequestMapping("port/quoteFastPrint/quoteprintfactory")
public class QuoteFastPrintControllerApi {
    @Autowired
    private QuoteFastPrintService quoteFastPrintService;

    @Autowired
    private QuotePrintFactoryPriceService quotePrintFactoryPriceService;

    @Autowired
    private QuotePrintService quotePrintService;

    /**
     * 列表
     */
    @RequestMapping("/list")
   /* @RequiresPermissions("quoteFastPrint:quoteprintfactory:list")*/
    public R list(@RequestBody Map paramBodyMap){
        //封装数码快印参数
        Map params = (Map) paramBodyMap.get("params");
        Map<String, List> map = new HashMap<>();
        //存放工厂打印机机器类型
        map.put("quotePrintFactoryEnum",JsonUtils.enumToList(QuotePrintFactoryEnum.class));
        //存放快印打印机颜色类型
        map.put("quoteFastPrintColourEnum",JsonUtils.enumToList(QuoteFastPrintColourEnum.class));
        PageUtils page = quoteFastPrintService.queryPage(params);
        return R.ok().put("page", page).put("userData",map);
    }



    /**
     * 信息
     */
    @RequestMapping("/info")
    /*@RequiresPermissions("quoteFastPrint:quoteprintfactory:info")*/
    public R info( @RequestBody Map paramBodyMap){
        Integer id = (Integer) paramBodyMap.get("id");
        //先定义一个包装类用于最终结果返回
        QuoteFastPrintEntityVo quoteFastPrintVo=new QuoteFastPrintEntityVo();
        //先从quote_print_factory这个表查
        QuoteFastPrintEntity quotefastPrint = quoteFastPrintService.selectById(id);
        //根据print_id查询quote_print
        QuotePrintEntity quotePrintEntity=quotePrintService.selectById(quotefastPrint.getPrintId());
        quotefastPrint.setPrintName(quotePrintEntity.getPrintName());
        //再从quote_print_factory_price这个表查
        List<QuotePrintFactoryPriceEntity>  quotePrintFacotoryPirceList=quotePrintFactoryPriceService.selectList(new EntityWrapper<QuotePrintFactoryPriceEntity>().eq("print_factory_id",id).eq("del_flag",DeleteEnum.NOT_DELETE.toInt()));
        quoteFastPrintVo.setQuotePrintFacotoryPirceList(quotePrintFacotoryPirceList);
        quoteFastPrintVo.setQuoteFastPrint(quotefastPrint);
        return R.ok().put("quoteFastPrintVo", quoteFastPrintVo);
    }

    /**
     * 修改
     */
    @RequestMapping("/updateWithPrice")
    /*@RequiresPermissions("quoteFastPrint:quoteprintfactory:update")*/
    public R update(@RequestBody  Map paramBodyMap){
        QuoteFastPrintEntityVo  quoteFastPrintEntityVo=JsonUtils.parseMap2Object((Map)paramBodyMap.get("quoteFastPrintEntityVo"),QuoteFastPrintEntityVo.class);
        //获取创建人
        String createUser= (String) paramBodyMap.get("createUser");
        return quoteFastPrintService.updateWithPrice(quoteFastPrintEntityVo,createUser);
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
  /*  @RequiresPermissions("quoteFastPrint:quoteprintfactory:delete")*/
    public R delete(@RequestBody  Map paramBodyMap){
        //解析参数
        List<Integer>  idsList= (List<Integer>) paramBodyMap.get("ids");
        Integer[] ids=idsList.toArray(new Integer[0]);
        String update_user= (String) paramBodyMap.get("update_user");
        //删除quote_print_factory表中数据，修改表中的del_flag字段
        //创建一个Map封装修改的参数
        Map<String,Object> map=new HashMap<String, Object>();
        map.put("update_user",update_user);
        quoteFastPrintService.deleteUpdateBatchIds(ids,map);
        //伪删除对应的quote_print_factory_price
        quotePrintFactoryPriceService.deleteUpdateBatchId(ids,map);
        return R.ok();
    }

    /**
     * 保存
     */
    @RequestMapping("/saveWithPrice")
   /* @RequiresPermissions("quoteFastPrint:quoteprintfactory:save")*/
    public R save(@RequestBody Map paramBodyMap){
        //解析参数
        QuoteFastPrintEntityVo  quotePrintFactoryVo=JsonUtils.parseMap2Object((Map)paramBodyMap.get("quotePrintFactoryVo"),QuoteFastPrintEntityVo.class);
        //获取创建人
        String createUser= (String) paramBodyMap.get("createUser");
        //获取factoryID
        Integer factoryId=Integer.valueOf(paramBodyMap.get("factoryId").toString());
        //先获取工厂信息
        QuoteFastPrintEntity quoteFastPrint=quotePrintFactoryVo.getQuoteFastPrint();
        if(quoteFastPrint!=null){
            //先根据printName查出print_id
            QuotePrintEntity quotePrintEntity=quotePrintService.selectOne(new EntityWrapper<QuotePrintEntity>().eq("print_name",quoteFastPrint.getPrintName()));
            //设置打印机的print_id
            quoteFastPrint.setPrintId(quotePrintEntity.getId());
            //设置打印机的print_type
            quoteFastPrint.setPrintType("shumakuaiyin");
            //设置创建时间
            quoteFastPrint.setCreateDate(new Date());
            //设置创建人
            quoteFastPrint.setCreateUser(createUser);
            //工厂id
            quoteFastPrint.setFactoryId(factoryId);
            //设置是否删除
            quoteFastPrint.setDelFlag(false);
            //名称修改了，但是数据仍插入到quote_print_factory这个表中
            quoteFastPrintService.insert(quoteFastPrint);
        }
        //再循环数据到插入价格表quote_print_factory_price
        List<QuotePrintFactoryPriceEntity> QuotePrintFactoryPriceList= quotePrintFactoryVo.getQuotePrintFacotoryPirceList();
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    if(QuotePrintFactoryPriceList.size()>0){
            for (QuotePrintFactoryPriceEntity quotePrintFactoryPriceEntity:QuotePrintFactoryPriceList) {
                //将print_factory_id的值设置为quoteFastPrint.id
                quotePrintFactoryPriceEntity.setPrintFactoryId(quoteFastPrint.getId());
                //设置创建时间
                quotePrintFactoryPriceEntity.setCreateDate(new Date());
                //设置创建人
                quotePrintFactoryPriceEntity.setCreateUser(createUser);
                //设置是否删除
                quotePrintFactoryPriceEntity.setDelFlag(false);
                //设置开机费
                quotePrintFactoryPriceEntity.setBeginPrice(new BigDecimal(0));
                //设置递增价格(以1000印数递增)
                quotePrintFactoryPriceEntity.setStepPrice(new BigDecimal(0));
                //设置是否为特种纸
                quotePrintFactoryPriceEntity.setCanUv(false);
                quotePrintFactoryPriceService.insert(quotePrintFactoryPriceEntity);
            }
        }
        return R.ok();
    }

    /**
     * 修改是否前端显示
     */
    @RequestMapping("/updateDisplay")
   /* @RequiresPermissions("quoteFastPrint:quoteprintfactory:info")*/
    public R updateDisplay(@RequestBody Map paramBodyMap){
        Integer id = (Integer) paramBodyMap.get("id");
        String updateUser=(String) paramBodyMap.get("update_user");
        //先根据id查询
        QuoteFastPrintEntity quoteFastPrintEntity=quoteFastPrintService.selectById(id);
        boolean display=!quoteFastPrintEntity.getDisplay();
        //修改是否展示
        quoteFastPrintEntity.setDisplay(display);
        quoteFastPrintEntity.setUpdateDate(new Date());
        quoteFastPrintEntity.setUpdateUser(updateUser);
        quoteFastPrintService.updateAllColumnById(quoteFastPrintEntity);
        return R.ok();
    }
}
